package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.MatchBean;
import cst.neu.yuejian_31.iface.MatchDetailIface;
import cst.neu.yuejian_31.iface.MatchDetailListener;
import cst.neu.yuejian_31.iface.MatchIface;
import cst.neu.yuejian_31.iface.MatchListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.MatchService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Alienware on 2017/11/3.
 */

public class MatchModel implements MatchIface,MatchDetailIface {
    private Retrofit retrofit;
    //    private String BASEURL="http://10.0.2.2/neu16_yuejian/api/Club/";
    public MatchModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getmatchList(final MatchListener matchListener) {
        MatchService matchService=retrofit.create(MatchService.class);
        Call<List<MatchBean>> call
                =matchService.getmatchlist();
        call.enqueue(new Callback<List<MatchBean>>() {
            @Override
            public void onResponse(Call<List<MatchBean>> call, Response<List<MatchBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                {
                    matchListener.onResponse(response.body());
                }
                else {
                    matchListener.onFail("FAIL");

                }
            }

            @Override
            public void onFailure(Call<List<MatchBean>> call, Throwable t) {

                matchListener.onFail(t.toString());
            }
        });
    }
    @Override
    public void getmatchdetail(String match_id,final MatchDetailListener matchDetailListener) {
        MatchService matchService=retrofit.create(MatchService.class);
        Call<MatchBean>call
                =matchService.getmatchdetail(match_id);
        call.enqueue(new Callback<MatchBean>() {
            @Override
            public void onResponse(Call<MatchBean> call, Response<MatchBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    matchDetailListener.onResponse(response.body());
                }
                else {
                    matchDetailListener.onFail("FAIL");
                }
            }

            @Override
            public void onFailure(Call<MatchBean> call, Throwable t) {

                matchDetailListener.onFail(t.toString());
            }
        });
    }
}